<?php
/**
 * BUDDYPRESS UNIT TEST SCRIPT - NAVIGATION -> ROUTER
 *
 * @version 1.6.1
 * @since 1.6.1
 * @package Unit Tests
 * @subpackage Navigation
 * @license GPL v2.0
 *
 * ========================================================================================================
 */

class navigation_router extends RAZ_testCase {


    	function setUp() {

		parent::setUp();
	}


	function test_bp_core_set_uri_globals() {

	    	    
		// This "unit test" is an example of an ANTI-PATTERN ...effectively "how not 
		// to write a unit test.
		// =========================================================================
	    
	    
		// Install BP's database tables
	    
		require_once( BP_PLUGIN_DIR . '/bp-core/admin/bp-core-schema.php' );
		
		$active_components = array (
					    'activity' => true,
					    'friends' => true,
					    'groups' => true,
					    'messages' => true,
					    'xprofile' => true,
					    'blogs'=> true
		);
		bp_core_install($active_components);
		
		bp_add_options();			
		
		// Add pages for BP components to WP install
						
		$pages = array(
					    'members' => 'Members',
					    'groups' => 'Groups',
					    'activity' => 'Activity',
					    'forums' => 'Forums',
					    'register' => 'Register',
					    'activate' => 'Activate'			    
		);
		
		$cls = new RAZ_wp();
		$bp_pages = array();
		
		foreach( $pages as $slug => $title ){
		    
			$page_id = $cls->addPage($title, $slug);						
			$bp_pages[$slug] = $page_id;
		
		}
		
		update_option( 'bp-pages', $bp_pages ); // update_option() creates the 
							// option if it doesn't already exist
		
		// Set active components
		
		$active_components = array( 
					    'xprofile' => true,
					    'settings' => true,
					    'friends' => true,
					    'messages' => true,
					    'activity' => true,
					    'groups' => true,
					    'forums' => true,
					    'blogs' => true,
					    'members' => true		    
		);
		
		update_option( 'bp-active-components', $active_components );
		
		$deactivated_components = array();
		
		update_option( 'bp-deactivated-components', $deactivated_components );	
		
		// ====================================================================
		// So at this point we have a complete, fully-loaded BP install with
		// all the necessary options set up
		// ====================================================================
		
		
		$_SERVER['REQUEST_URI'] = "/members/admin/foo/bar/baz";

		bp_core_set_uri_globals();
		
		global $bp;
		
		$result = array(
				'current_component' => $bp->current_component,
				'current_action' => $bp->current_action,
				'current_item' => $bp->current_item,
				'action_variables' => $bp->action_variables		    
		);
		
		// RAZ_debug::dump($result);  
 
		
		// But why doesn't it work ??? 
		// ===============================================================================
		//
		// 1) bp_core_set_uri_globals() follows a monolithic design and is entangled with
		// huge numbers of global functions throughout the WP and BP cores
		//
		// 2) Because functions in PHP can't be removed from the global namespace after
		// they've been declared, they can't be replaced with mock versions for testing
		//
		// 3) Because bp_core_set_uri_globals() has a very opaque design. Its not broken
		// into functions which accomplish tasks, and the documentation inside the function
		// explains 'tasks' instead of how the design fits together. When a typical coder
		// looks at this function, they have no clue how it works.
		//
		// 4) Its impossibe to write *unit tests* for bp_core_set_uri_globals() because
		// it can't be *isolated* as a functional unit; and it would be extremely difficult
		// to write *any* sort of tests for it due to its opaque design and heavy dependencies.
		// 
		// 5) bp_core_set_uri_globals() has no error handler. Its impossible to tell when it
		// hits an error condition because it doesn't relay this information to the caller
		// 
		// 6) http://en.wikipedia.org/wiki/State_machine despite being a state machine, 
		// bp_core_set_uri_globals() has no internal state variable. This makes the function 
		// a 'black box', requiring developers to paste 'metrology code' into the function 
		// every time they want to find out why its not working properly.
		//
		// 7) See the next example for how to "do it right"
		//
		// ===============================================================================
		
	}

	function test_BPM_router() {
	    
		// We're currently building this one...
		return true;
	    
	}


	function tearDown() {
		parent::tearDown();
	}
	
}

?>